﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using SchoolManagementSystem.Details;
using CoreData;
using ActionServices;
using SchoolManagementSystem.Controls;

namespace SchoolManagementSystem.Forms.BusinessForm
{
    public partial class Stock_VoucherIn_List : DevExpress.XtraEditors.XtraForm
    {
        public Stock_VoucherIn_List()
        {
            InitializeComponent();
            LoadStaff();
            LoadStock();
            LoadInputMethod();
            LoadPaymentMethod();
           
            dateEdit_from.DateTime = DateTime.Now.AddMonths(-6);
            dateEdit_to.DateTime = DateTime.Now;
            Binding();

        }
        void LoadList(string _searchstring)
        {
            Stock_VoucherIn stock_voucherdata = new Stock_VoucherIn();//field filter here
            IList<Stock_VoucherIn> listdata = (IList<Stock_VoucherIn>)RegisterService.RegisterServices.StockVoucherInService.GetListByFilter(_searchstring);//Stock_VoucherIn SV
            gridControl1.DataSource = listdata;
            gridControl1.RefreshDataSource();
        }        
        public BaseActionService Model { get; set; }
        private void simpleButton2_Click(object sender, EventArgs e)
        {

        }

        private void barButtonItem_new_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Stock_VoucherInForm frm = new Stock_VoucherInForm();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Binding();
                SelectRow(frm);
            }
        }

        private void SelectRow(Stock_VoucherInForm frm)
        {
            for (int i = 0; i < gridView1.RowCount; i++)
            {
                if (((long)(gridView1.GetRowCellValue(i, "ID"))) == frm.ID)
                {
                    gridView1.SelectRow(i);
                }
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sender;

            Point pt = view.GridControl.PointToClient(Control.MousePosition);

            DoRowDoubleClick(view, pt);
        }
        private static void DoRowDoubleClick(DevExpress.XtraGrid.Views.Grid.GridView view, Point pt)
        {

            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info = view.CalcHitInfo(pt);

            if (info.InRow || info.InRowCell)
            {
                //string colCaption = info.Column == null ? "N/A" : info.Column.GetCaption();

                //MessageBox.Show(string.Format("DoubleClick on row: {0}, column: {1}.", info.RowHandle, colCaption));
                long id = Convert.ToInt64(view.GetFocusedRowCellValue("ID"));
                Stock_VoucherInForm frm = new Stock_VoucherInForm();
                frm.ShowDialog(id);
            }

        }
        void LoadStaff()
        {
            try
            {
                var data = (List<Staff>)RegisterService.RegisterServices.StaffService.GetList();
                data.Insert(0, new Staff()
                {
                    Name = "Tất cả",
                    ID = 0
                });

                lookUpEdit_Employee.Properties.DataSource = data;
                lookUpEdit_Employee.Properties.ValueMember = "ID";
                lookUpEdit_Employee.Properties.DisplayMember = "Name";
                string[] columns = { "ID", "Name" };
                for (int i = 0; i < lookUpEdit_Employee.Properties.Columns.Count; i++)
                {
                    {
                        var index = Array.IndexOf(columns, lookUpEdit_Employee.Properties.Columns[i].FieldName);
                        if (index < 0)
                        {
                            lookUpEdit_Employee.Properties.Columns[i].Visible = false;
                            lookUpEdit_Employee.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                        }
                    }
                }
            }
            catch { }
        }

        void LoadPaymentMethod()
        {
            try
            {
                var data = (List<Payment_Method>)RegisterService.RegisterServices.PaymentMethodService.GetList();
                data.Insert(0, new Payment_Method()
                {
                    Name = "Tất cả",
                    ID = 0
                });

                lookUpEdit_PaymentMethod.Properties.DataSource = data;
                lookUpEdit_PaymentMethod.Properties.ValueMember = "ID";
                lookUpEdit_PaymentMethod.Properties.DisplayMember = "Name";
                string[] columns = { "ID", "Name" };
                for (int i = 0; i < lookUpEdit_PaymentMethod.Properties.Columns.Count; i++)
                {
                    {
                        var index = Array.IndexOf(columns, lookUpEdit_PaymentMethod.Properties.Columns[i].FieldName);
                        if (index < 0)
                        {
                            lookUpEdit_PaymentMethod.Properties.Columns[i].Visible = false;
                            lookUpEdit_PaymentMethod.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                        }
                    }
                }
            }
            catch { }
        }

        void LoadInputMethod()
        {
            try
            {
                var data = (List<Stock_Method>)RegisterService.RegisterServices.StockMethodService.GetListByLINQ(c=>c.ID==1);
                data.Insert(0, new Stock_Method()
                {
                    Name = "Tất cả",
                    ID = 0
                });

                lookUpEdit_InputMethod.Properties.DataSource = data;
                lookUpEdit_InputMethod.Properties.ValueMember = "ID";
                lookUpEdit_InputMethod.Properties.DisplayMember = "Name";
                string[] columns = { "ID", "Name" };
                for (int i = 0; i < lookUpEdit_InputMethod.Properties.Columns.Count; i++)
                {
                    {
                        var index = Array.IndexOf(columns, lookUpEdit_InputMethod.Properties.Columns[i].FieldName);
                        if (index < 0)
                        {
                            lookUpEdit_InputMethod.Properties.Columns[i].Visible = false;
                            lookUpEdit_InputMethod.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                        }
                    }
                }
            }
            catch { }
        }

        void LoadStock()
        {
            try
            {
                var data = (List<Product_Warehouse>)RegisterService.RegisterServices.ProductWareHouseService.GetList();
                data.Insert(0, new Product_Warehouse()
                {
                    Name = "Tất cả",
                    ID = 0
                });

                lookUpEdit_Warehouse.Properties.DataSource = data;
                lookUpEdit_Warehouse.Properties.ValueMember = "ID";
                lookUpEdit_Warehouse.Properties.DisplayMember = "Name";
                string[] columns = { "ID", "Name" };
                for (int i = 0; i < lookUpEdit_Warehouse.Properties.Columns.Count; i++)
                {
                    {
                        var index = Array.IndexOf(columns, lookUpEdit_Warehouse.Properties.Columns[i].FieldName);
                        if (index < 0)
                        {
                            lookUpEdit_Warehouse.Properties.Columns[i].Visible = false;
                            lookUpEdit_Warehouse.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                        }
                    }
                }
            }
            catch { }
        }

        private void cmdtim_Click(object sender, EventArgs e)
        {

            Binding();
        }

        private void Binding()
        {
            string search = "";
            var datefrom = dateEdit_from.DateTime;
            var dateto = dateEdit_to.DateTime;
            var employee = lookUpEdit_Employee.EditValue != null ? lookUpEdit_Employee.EditValue.ToString() : "";
            var paymentmethod = lookUpEdit_PaymentMethod.EditValue != null ? lookUpEdit_PaymentMethod.EditValue.ToString() : "";
            var inputmethod = lookUpEdit_InputMethod.EditValue != null ? lookUpEdit_InputMethod.EditValue.ToString() : "";
            var warehouse = lookUpEdit_Warehouse.EditValue != null ? lookUpEdit_Warehouse.EditValue.ToString() : "";
            long employeeid = 0;
            long paymentmethodid = 0;
            long inputmethodid = 0;
            long warehouseid = 0;
            if (lookUpEdit_Employee.EditValue != null)
            {
                employeeid = (long)(lookUpEdit_Employee.EditValue);
            }
            if (lookUpEdit_PaymentMethod.EditValue != null)
            {
                paymentmethodid = (long)(lookUpEdit_PaymentMethod.EditValue);
            }
            if (lookUpEdit_InputMethod.EditValue != null)
            {
                inputmethodid = (long)(lookUpEdit_InputMethod.EditValue);
            }
            if (lookUpEdit_Warehouse.EditValue != null)
            {
                warehouseid = (long)(lookUpEdit_Warehouse.EditValue);
            }
            search += "Convert(varchar(11), SV.VoucherDate, 120) BETWEEN '" + datefrom.ToString("yyyy-MM-dd") + "' and '" + dateto.ToString("yyyy-MM-dd") + "'";
            if (!string.IsNullOrEmpty(txtVoucherNo.Text))
            {
                search += "and SV.VoucherInNo like '%" + txtVoucherNo.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtBillNo.Text))
            {
                search += "and SV.BillNo like '%" + txtBillNo.Text + "%'";
            }
            if (employeeid > 0)
            {
                search += "and SV.EmployeeID=" + employeeid + "";
            }
            if (paymentmethodid > 0)
            {
                search += "and SV.Payment_MethodID=" + paymentmethodid + "";
            }
            if (inputmethodid > 0)
            {
                search += "and SV.Input_MethodID=" + inputmethodid + "";
            }
            if (warehouseid > 0)
            {
                search += "and SV.WarehouseID=" + warehouseid + "";
            }
            if (datefrom > dateto)
            {
                MessageControl.ShowMessage("Khoảng thời gian ko hợp lệ", "Error", MessageControl.Icon.Error);
                return;
            }
            if (!string.IsNullOrEmpty(textEdit_Note.Text))
            {
                search += "and SV.Note like N'%" + textEdit_Note.Text + "%'";
            }
            if (String.IsNullOrEmpty(search))
                search = " 1=1";
            LoadList(search);
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                DeleteRow();   
            }

        }
        void DeleteRow()
        {
            if (gridView1.SelectedRowsCount > 0)
            {
                if (MessageBox.Show("Bạn có chắc muốn xóa dữ liệu đã chọn trong danh sách?", "Xóa dữ liệu", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                {
                    long objID = (long)gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ID");
                    Stock_VoucherIn data = new Stock_VoucherIn() { ID = objID };
                    RegisterService.RegisterServices.StockVoucherInService.UpdateIsDelete(data);
                    gridView1.DeleteSelectedRows();
                }
            }
            else
            {
                MessageBox.Show("Chọn dữ liệu cần xóa trong danh sách", "Xóa dữ liệu", MessageBoxButtons.OK);
            }
        }

        private void barButtonItem_delete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteRow();
        }
    }
}